<template>
  <el-dialog :title="$t('productCenter.appConfig.versionUpdate.add')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="250px" class="demo-ruleForm">
      <el-form-item :label="$t('productCenter.appConfig.versionUpdate.type')" prop="forceUpdate">
        <el-select v-model="formValidate.forceUpdate" clearable :placeholder="$t('productCenter.appConfig.versionUpdate.pleaseType')" style="width: 100%">
          <el-option
            v-for="item in $t('productCenter.appConfig.versionUpdate.updateChoose')"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('productCenter.appConfig.versionUpdate.newVersion')" prop="newVersion">
        <el-input
          v-model="formValidate.newVersion"
          clearable
          :placeholder="$t('productCenter.appConfig.versionUpdate.pleaseNewVersion')"
          :maxlength="50"
          @change="versionReal"
        />
      </el-form-item>
      <el-form-item :label="$t('productCenter.appConfig.versionUpdate.updateLog')" prop="updateLog">
        <el-input
          v-model="formValidate.updateLog"
          type="textarea"
          :autosize="{ minRows: 6, maxRows: 10}"
          :placeholder="$t('common.input')"
          :maxlength="500"
          show-word-limit
        />
      </el-form-item>
      <el-form-item :label="$t('productCenter.appConfig.versionUpdate.serverUrl')" prop="serverUrl">
        <el-input
          v-model="formValidate.serverUrl"
          clearable
          :placeholder="$t('common.input')"
          :maxlength="100"
        />
      </el-form-item>
      <el-form-item :label="$t('productCenter.appConfig.versionUpdate.googleUrl')" prop="googleUrl">
        <el-input
          v-model="formValidate.googleUrl"
          clearable
          :placeholder="$t('common.input')"
          :maxlength="100"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addVersion } from '@/api/product'
export default {
  name: 'ContactInfoDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: this.params(),
      rules: {
        forceUpdate: [{ required: true, type: 'number', message: this.$t('productCenter.appConfig.versionUpdate.type') + this.$t('common.null'), trigger: 'blur' }],
        newVersion: [{ required: true, message: this.$t('productCenter.appConfig.versionUpdate.newVersion') + this.$t('common.null'), trigger: 'blur' }],
        updateLog: [{ required: true, message: this.$t('productCenter.appConfig.versionUpdate.updateLog') + this.$t('common.null'), trigger: 'blur' }],
        serverUrl: [{ required: true, message: this.$t('productCenter.appConfig.versionUpdate.serverUrl') + this.$t('common.null'), trigger: 'blur' }],
        googleUrl: [{ required: true, message: this.$t('productCenter.appConfig.versionUpdate.googleUrl') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  methods: {
    params() {
      return {
        forceUpdate: null,
        newVersion: null,
        updateLog: null,
        serverUrl: null,
        googleUrl: null
      }
    },
    handleClose() {
      this.$emit('change-dialog', false)
      setTimeout(() => {
        this.formValidate = this.params()
      }, 10)
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = this.formValidate
          addVersion(data)
            .then(res => {
              this.submitLoading = false
              if (res.success) {
                this.$message.success(this.$t('common.tip.addSuccess'))
                this.$emit('change-dialog', false)
                this.$emit('edit-success')
                this.handleClose()
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.submitLoading = false
            })
        }
      })
    },
    versionReal() {
      const real = /^V\w{1,9}\.\w{1,2}\.\w{1,2}$/
      if (!real.test(this.formValidate.newVersion)) {
        this.formValidate.newVersion = ''
      }
    }
  }
}
</script>

<style scoped>

</style>
